<html>
<head><meta charset="utf-8"><title>Unsafe version of Any trait? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html">Unsafe version of Any trait?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217183892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217183892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217183892">(Nov 18 2020 at 19:23)</a>:</h4>
<p>Is there a way to have a <code>dyn Anything</code> in the sense of an owned value but without the type id stuff (so downcasting is unsafe like a union)? This would usually be a <code>void*</code> in C, although I guess that's a bit ambiguous about ownership.</p>



<a name="217184144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217184144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217184144">(Nov 18 2020 at 19:25)</a>:</h4>
<p>So like a type for <code>mybox.into_raw().cast::&lt;()&gt;()</code>?</p>
<p>I don't think I've ever seen a type for such a thing.</p>



<a name="217184222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217184222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217184222">(Nov 18 2020 at 19:25)</a>:</h4>
<p>It's a bit awkward for that to be "owned" since it clearly can't free itself properly, but I guess that's no worse than <code>ManuallyDrop&lt;Vec&lt;_&gt;&gt;</code>.</p>



<a name="217184450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217184450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217184450">(Nov 18 2020 at 19:27)</a>:</h4>
<p>I suppose it could track the size of the allocation, not sure how that stacks up compared to <code>TypeId</code> in terms of space &amp; efficiency</p>



<a name="217184559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217184559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217184559">(Nov 18 2020 at 19:27)</a>:</h4>
<p>actually I guess with drop glue you probably have to keep the type around if you want drop to work properly</p>



<a name="217184611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217184611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217184611">(Nov 18 2020 at 19:28)</a>:</h4>
<p>That'd still make it fat, yeah, so I don't think it's worth doing vs just having the vtable.</p>



<a name="217184835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217184835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217184835">(Nov 18 2020 at 19:29)</a>:</h4>
<p>For the <code>void*</code> alternative, I recall folks using <code>*const ()</code> for a while and then going to extern types, although neither of those really expresses <code>union { Everything }</code></p>



<a name="217185032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185032">(Nov 18 2020 at 19:31)</a>:</h4>
<p>yeah, an extern type would be better than <code>()</code>, but I don't remember if they exist yet :P</p>



<a name="217185038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185038">(Nov 18 2020 at 19:31)</a>:</h4>
<p>unstable</p>



<a name="217185054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185054">(Nov 18 2020 at 19:31)</a>:</h4>
<p>(due to the size_of_val mess)</p>



<a name="217185162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185162">(Nov 18 2020 at 19:32)</a>:</h4>
<p>Maybe it's just <code>Box&lt;dyn Whatever&gt;</code>, <span class="user-mention" data-user-id="271719">@Mario Carneiro</span>, with <code>trait Whatever {} impl&lt;T&gt; Whatever for T {}</code>.</p>



<a name="217185241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185241">(Nov 18 2020 at 19:32)</a>:</h4>
<p>how do you downcast that?</p>



<a name="217185320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185320">(Nov 18 2020 at 19:33)</a>:</h4>
<p><code>&amp;mut *(Box::into_raw(foo) as *mut MyType)</code>, for example</p>



<a name="217185345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185345">(Nov 18 2020 at 19:33)</a>:</h4>
<p>is it safe to just get the pointer and cast it, or would the vtable pointer make that unsound</p>



<a name="217185377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185377">(Nov 18 2020 at 19:34)</a>:</h4>
<p>because it's a fat pointer, right?</p>



<a name="217185421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185421">(Nov 18 2020 at 19:34)</a>:</h4>
<p>I <em>think</em> we guarantee that the pointer cast of an unsized to sized type gives you the data ptr, but I'm not sure on that</p>



<a name="217185449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185449">(Nov 18 2020 at 19:34)</a>:</h4>
<p>(and that example should be as_ptr(), not into_raw, I guess)</p>



<a name="217185582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217185582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217185582">(Nov 18 2020 at 19:35)</a>:</h4>
<p>I can't find it immediately in reference</p>



<a name="217190168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafe%20version%20of%20Any%20trait%3F/near/217190168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafe.20version.20of.20Any.20trait.3F.html#217190168">(Nov 18 2020 at 20:11)</a>:</h4>
<p>Certainly that's what we do with slice pointers; it's probably the same for other fat pointers too.  (Not something I've done.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>